package com.jyh.service.loan;


import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import com.jyh.config.BaseAction;
import com.jyh.config.datasource.TargetDataSource;
import com.jyh.dbOrder.dao.LoanDao;
import com.jyh.service.ButtonService;
import com.jyh.db.entity.JyhOrder;
import com.jyh.db.entity.JyhOrderExample;
import com.jyh.db.entity.JyhOrderLog;
import com.jyh.db.mapper.JyhOrderLogMapper;
import com.jyh.db.mapper.JyhOrderMapper;
import com.jyh.util.Constant;
import com.jyh.util.StringUtils;

@Service
public class ManualAditLoanService extends BaseAction {
	@Autowired
	private LoanDao loanDao; 
	@Autowired
	private JyhOrderMapper jyhOrderMapper;
	@Autowired
	private JyhOrderLogMapper jyhOrderLogMapper;
	@Autowired
	private ButtonService buttonService;
	
	/**
	 * 获取带人工审核页面数据
	 * @param request
	 * @param session
	 * @return
	 */
	@TargetDataSource(name = "order")
	public ModelAndView getManualAdit(HttpServletRequest request, HttpSession session) {
		ModelAndView mv = new ModelAndView("loan/manualAdit");
		String userNO  = request.getParameter("userNO");
		String orderNO  = request.getParameter("orderNO");
		String userName  = request.getParameter("userName");
		String score1  = request.getParameter("score1");
		String score2  = request.getParameter("score2");
		String orderTime1  = request.getParameter("orderTime1");
		String orderTime2  = request.getParameter("orderTime2");
		String operationTime1  = request.getParameter("operationTime1");
		String operationTime2  = request.getParameter("operationTime2");
		
		String page = request.getParameter("currentPage");// 传过来的页数
		int lineSize = 20; // 每页显示记录20条数
		int pageCount = 0; // 总页数
		int count = 0; // 数据总数
		int currentPage = 1; // 当前是第几页,默认是第一页
		if (page == null || "1".equals(page)) {
		} else {
			currentPage = Integer.parseInt(page); // 当前是第几页,默认是第一页
		}
		if (currentPage < 1) {
			currentPage = 1;
		}
		String limitSql = " limit " + (currentPage - 1) * lineSize + "," + lineSize;
		String sql1 = "select count(1) as count from jyh_order t1 left join jyh_loan_type t3 on t1.typeNO=t3.typeNO"
				  + " left join jyh_user_msg t4 on t1.orderNO=t4.orderNo where 1=1";
		
		String sql = "select t4.userName,t1.*,t3.* from jyh_order t1 left join jyh_loan_type t3 on t1.typeNO=t3.typeNO"
				  + " left join jyh_user_msg t4 on t1.orderNO=t4.orderNo where 1=1";
		if (StringUtils.isNotNull(userNO)) {
			sql = sql + " and t1.userNO='" + userNO + "'";
			sql1 = sql1 + " and t1.userNO='" + userNO + "'";
		}
		if (StringUtils.isNotNull(orderNO)) {
			sql = sql + " and t1.orderNO='" + orderNO + "'";
			sql1 = sql1 + " and t1.orderNO='" + orderNO + "'";
		}
		if (StringUtils.isNotNull(userName)) {
			sql = sql + " and t4.userName<='" + userName + "'";
			sql1 = sql1 + " and t4.userName<='" + userName + "'";
		}
		if (StringUtils.isNotNull(score1)) {
			sql = sql + " and t1.score>='" + score1 + "'";
			sql1 = sql1 + " and t1.score>='" + score1 + "'";
		}
		if (StringUtils.isNotNull(score2)) {
			sql = sql + " and t1.score<='" + score2 + "'";
			sql1 = sql1 + " and t1.score<='" + score2 + "'";
		}
		if (StringUtils.isNotNull(orderTime1)) {
			sql = sql + " and t1.orderTime>='" + orderTime1 + "'";
			sql1 = sql1 + " and t1.orderTime>='" + orderTime1 + "'";
		}
		if (StringUtils.isNotNull(orderTime2)) {
			sql = sql + " and t1.orderTime<='" + orderTime2 + "'";
			sql1 = sql1 + " and t1.orderTime<='" + orderTime2 + "'";
		}
		if (StringUtils.isNotNull(operationTime1)) {
			sql = sql + " and t1.operationTime>='" + operationTime1 + "'";
			sql1 = sql1 + " and t1.operationTime>='" + operationTime1 + "'";
		}
		if (StringUtils.isNotNull(operationTime2)) {
			sql = sql + " and t1.operationTime<='" + operationTime2 + "'";
			sql1 = sql1 + " and t1.operationTime<='" + operationTime2 + "'";
		}
		
		sql1 = sql1 + " and t1.state='" + Constant.ORDER_STATE_3 + "'";
		Map<String, Object> map = loanDao.getMap(sql1);
		count = Integer.parseInt(map.get("count") + "");
		if(count<=lineSize){
			limitSql = " limit 0," + lineSize;
		}
		sql = sql + " and t1.state='" + Constant.ORDER_STATE_3 + "' order by t1.reviewTime desc" + limitSql;
		List<Map<String, Object>> list = loanDao.getList(sql);
		
		int temp = count / lineSize; // 取商
		int tem2 = count % lineSize;// 取余数
		if (tem2 > 0 && tem2 < lineSize && temp > 0) {
			pageCount = count / lineSize + 1; // 总页数
		} else if (tem2 == 0 && temp > 0) {
			pageCount = temp;
		} else {
			pageCount = 1; // 总页数
		}
		if (currentPage >= pageCount) {
			currentPage = pageCount;
		}
		list = (List<Map<String, Object>>) buttonService.getTableButton(5, "orderNO,userNO", jsonOutSimplify(list), session);
		mv.addObject("pageCount", pageCount);
		mv.addObject("currentPage", currentPage);
		mv.addObject("count", count);
		mv.addObject("list", list);
		//将传递过来的参数回显
		mv.addObject("orderNO", orderNO);
		mv.addObject("userName", userName);
		mv.addObject("userNO", userNO);
		mv.addObject("operationTime1", operationTime1);
		mv.addObject("operationTime2", operationTime2);
		mv.addObject("score1", score1);
		mv.addObject("score2", score2);
		mv.addObject("orderTime1", orderTime1);
		mv.addObject("orderTime2", orderTime2);
		return mv;
	}
	
	/**
	 * 订单状态变更 接口(pass通过 no拒贷)
	 */
	@TargetDataSource(name = "order")
	public void changeState(HttpServletRequest request, HttpSession session) {
		// 订单即将变更为xx状态
		String status = request.getParameter("status");
		int state = Constant.ORDER_STATE_3;
		String orderNO = request.getParameter("orderNO");
		if (StringUtils.isNull(status)) {
			logger.error("修改状态status不能为空");
		}
		if (StringUtils.isNull(orderNO)) {
			logger.error("订单编号orderNO不能为空");
		}
		if("pass".equals(status)){
			state = Constant.ORDER_STATE_5;
		}else if("no".equals(status)){
			state = Constant.ORDER_STATE_4;
		}
		JyhOrderExample example = new JyhOrderExample();
		example.createCriteria().andOrderNOEqualTo(orderNO);
		JyhOrder order = new JyhOrder();
		order.setState(state);
		order.setOperationTime(new Date());
		order.setOperatorName("SYSTEM");
		order.setOperatorNO("888888");
		int count = jyhOrderMapper.updateByExampleSelective(order, example);
		if (count <= 0) {
			logger.error("订单orderNO=[{}]，订单更新失败", orderNO);
		} 
		// 订单操作记录入库
		JyhOrderLog jyhOrderLog = new JyhOrderLog();
		jyhOrderLog.setOrderNO(orderNO);
		jyhOrderLog.setState(state);
		jyhOrderLog.setOperationTime(new Date());
		jyhOrderLog.setOperatorName("SYSTEM");
		jyhOrderLog.setOperatorNO("888888");
		int num = jyhOrderLogMapper.insertSelective(jyhOrderLog);
		if (num <= 0) {
			logger.error("订单orderNO=[{}]，订单操作记录更新失败", orderNO);
		}
	}

}
